<%--
  Created by IntelliJ IDEA.
  User: xu
  Date: 2016/1/18
  Time: 9:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传</title>
    <%@include file= "/WEB-INF/pages/base/commImportPage.jsp" %>
    <script src="<%=rootPath%>/js/plupload/plupload.full.min.js" type="text/javascript"></script>
    <%--<script src="<%=rootPath%>/js/plupload/jquery.plupload.queue/jquery.plupload.queue.min.js" type="text/javascript"></script>--%>
    <script src="<%=rootPath%>/js/plupload/i18n/zh_CN.js" charset="UTF-8" type="text/javascript"></script>
    <%--<link rel="stylesheet" type="text/css" href="<%=rootPath%>/js/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css">--%>
</head>
<body>
<div class="modal-body" style="height: 400px">
    <div id="filelist">
        <p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
    </div>

    <div>
        <div class="dr-succc">
            <div class="dr-succc">
                <p class="p-dr0">选择文件</p>
                <p class="p-dr1">将文件添加到上传队列，然后点击“开始上传”按钮。</p>
                <div class="dr-table">
                    <table cellpadding="0" cellspacing="0" id="filelistTable">
                        <tr>
                            <th>文件名</th>
                            <th><img src="${resourcePath}/images/guandaoqingli/folder.png" alt=""/>&nbsp;&nbsp;文件大小</th>
                            <th>上传进度</th>
                            <th>导入状态</th>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <div class="dr-list" >
            <div id="container">
            <a id="pickfiles" href="javascript:void(0)" class="a1"></a>
            <a id="uploadfiles" href="javascript:void(0)" class="a2"></a>
            </div>
            <ul id="uploadSuccess" style="display: none">
                <li><em class="ok"></em><span>导入记录成功！</span></li>
            </ul>
            <ul name="importErr" style="display: none">
                <li><em class="no"></em><span>数据导入失败！共<i id="errNum">0</i>处错误,下载 <a href="javascript:void(0)" onclick=downErrM()>所有错误详情</a></span></li>
            </ul>
            <p name="importErr" class="error-info" style="display: none">错误详情</p>
            <div class="no-info" name="importErr" id="importErr" style="display: none">

            </div>
        </div>
    </div>
 </div>
    <div class="modal-footer" style="text-align:center;">
        <button class="btn btn-w100 btn-save-icon1 btn-h40" id="ddd" onclick="closeW()"><em style="top:10px;left:35px"></em>关闭</button>
    </div>

<script type="text/javascript">
    $(document).ready(function(){
        initUp();
    });

    var api = frameElement.api, W = api.opener;
    function initUp(){
        //http://www.plupload.com/docs/Uploader#wiki-methods
        var data=api.data;
        var upPath=data.upUrl;
    if (upPath==null||upPath==""){
        alert('没有上传地址，无法初始化上传控件');
        return;
    }

        if(upPath.indexOf("?")==-1){
            upPath=upPath+"?uploadFile=yes";
        }else {
            upPath=upPath+"&uploadFile=yes";
        }
        var uploader = new plupload.Uploader({
            runtimes : 'html5,flash,silverlight,html4',
            browse_button : 'pickfiles', // you can pass in id...
            container: document.getElementById('container'), // ... or DOM Element itself
            chunk_size : '10mb',
            url :p_path+ upPath,
            filters : {
                max_file_size : '50mb',
                mime_types: [
                    {title : "excel files", extensions : "xls,xlsx"}
                ]
            },
            flash_swf_url : '/plupload/js/Moxie.swf',
            silverlight_xap_url : '/plupload/js/Moxie.xap',
            init: {
                PostInit: function() {
                    document.getElementById('filelist').innerHTML = '';
                    document.getElementById('uploadfiles').onclick = function() {
                        uploader.start();
                        return false;
                    };
                },
                BeforeUpload:function(uploader,file){
                    $("#importErr").append("<span>"+file.name+">>开始上传>></span></br>");
                },
                FilesAdded: function(up, files) {
                    plupload.each(files, function(file) {
                        var fListtr="<tr id='"+file.id+"'>" +
                                "<td class=\"bd\">"+file.name+"</td>"+
                        "<td class=\"bd\">"+plupload.formatSize(file.size)+"</td>"+
                                "<td name=progress class=\"bd\"></td>"+
                                "<td name=result class=\"bd\"></td>";
                        $("#filelistTable").append(fListtr);
                       // document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
                    });
                },

                UploadProgress: function(up, file) {
                    $("td[name=progress]","#"+file.id).html(file.percent+"%")
                    //document.getElementById(file.id).innerHTML = '<span>' + file.percent + "%</span>";
                },

                Error: function(up, err) {
                    console.log(err.response)
                    var resp=err.response;
                    var res= $.parseJSON(resp);
                    $("#importErr").append("<p>"+res.error.message+"</p></br>");
                    $("td[name=result]","#"+err.file.id).addClass("bd color-r").html("失败");
                    $("*[name=importErr]").show();
                },
                UploadComplete:function(up,fils){
                    upPath="";
                    //var fNum=$(".color-r","#"+file.id).length;
                    var i1=0;
                    $("p","#importErr").each(function(i,d){
                        var htm=$(d).html();
                        var ii=getlen(htm,"<br>");
                        i1=(parseInt(i1)+parseInt(ii));
                    });
                    $("#errNum").html(i1);
                    if (i1>0){
                        $("#uploadSuccess").hide();
                    }


                },
                FileUploaded: function(up, file, info) {
                    // Called when file has finished uploading
                    $("td[name=result]","#"+file.id).addClass("bd color-g").html("成功");
                    var fNum=$(".color-r","#"+file.id).length;
                    if (fNum>0){
                        $("#uploadSuccess").hide();
                    }else {
                        $("#uploadSuccess").show();
                    }
                    $("#importErr").append("<span>"+file.name+">>上传成功!</span></br>");
                },
            }
        });

        uploader.init();
    // Setup html5 version
    /*$("#uploader").pluploadQueue({
        // General settings
        runtimes : 'html5,flash,silverlight,html4',
        url : p_path+upPath,
        chunk_size : '10mb',
        rename : true,
        dragdrop: true,

        filters : {
            // Maximum file size
            max_file_size : '50mb',
            // Specify what files to browse for
            mime_types: [
                //{title : "Image files", extensions : "jpg,gif,png"},
                {title : "excel files", extensions : "xls,xlsx"}
            ]
        },

        // Resize images on clientside if we can
        resize: {
            width : 200,
            height : 200,
            quality : 90,
            crop: true // crop to exact dimensions
        },


        // Flash settings
        flash_swf_url : '/js/plupload/Moxie.swf',

        // Silverlight settings
        silverlight_xap_url : '/js/plupload/Moxie.xap',

        init:{

            FileUploaded: function(up, file, info) {
                // Called when file has finished uploading

                $(".no-info",$("#importErr")).append("<p>"+"上传成功！"+"</p></br>");
            },
            Error:function(code,message,file,response,ststus,responseHeaders ){
                var resp=message.response;
                var res= $.parseJSON(resp);
                $(".no-info",$("#importErr")).append("<p>"+res.error.message+"</p></br>");
                $("#importErr").show();

            },
            BeforeUpload:function(uploader,file){
                $(".no-info",$("#importErr")).append("<p>"+file.name+">>开始上传:</p>");
            },
            UploadComplete:function(up,fils){
                upPath="";
            }
        }
    });*/}

    function showErrmessage(){
        $("#errDiv").toggle();
    }

    function closeW(){
        W.uploadDialog_.close();
    }

    function downErrM(){
        var url=p_path+"/public/down/downExcelMessage.json";
        var ipt=$("<input name='html' value=''>").val($("#importErr").html());
        backgroundExportByForm.putDomToForm(ipt,url);
        backgroundExportByForm.submit();
    }

</script>
</body>
</html>
